/**
 * @Author ZhangCuirong
 * @Date 2025/9/9 10:20
 * @description:
 */
public class Solution468 {
    public boolean validIPV4Address(String queryIP) {
        String[] ipv4s = queryIP.split("\\.");
        if (queryIP.endsWith(".") || queryIP.startsWith(".")) {
            return false;
        }
        if (ipv4s.length != 4) {
            return false;
        }
        for (String ipv4 : ipv4s) {
            if (!(ipv4.length() >= 1 && ipv4.length() <= 3)) {
                return false;
            }
            //检验前导0
            if (ipv4.length() >= 2 && ipv4.charAt(0) == '0') {
                return false;
            }
            //判断数字
            for (int i = 0; i < ipv4.length(); i++) {
                if (!Character.isDigit(ipv4.charAt(i))) {
                    return false;
                }
            }
            int digit = Integer.parseInt(ipv4);
            if (!(digit >= 0 && digit <= 255)) {
                return false;
            }

        }
        return true;
    }

    public boolean validIPV6Address(String queryIP) {
        if (queryIP.endsWith(":") || queryIP.startsWith(":")) {
            return false;
        }
        String[] ipv6s = queryIP.split(":");
        if (ipv6s.length != 8) {
            return false;
        }
        for (String ipv6 : ipv6s) {
            //检验长度
            if (!(ipv6.length() >= 1 && ipv6.length() <= 4)) {
                return false;
            }
            //判断数字字母
            for (int i = 0; i < ipv6.length(); i++) {
                char c = ipv6.charAt(i);
                if (!( (c >= '0' && c <= '9')
                        || (c >= 'a' && c <= 'f')
                        || (c >= 'A' && c <= 'F') )) {
                    return false;
                }
            }
            //判断是合法的16进制

        }
        return true;
    }

    public String validIPAddress(String queryIP) {
        if (queryIP.contains(":")) {
            if (validIPV6Address(queryIP)) {
                return "IPv6";
            } else {
                return "Neither";
            }
        }
        if (queryIP.contains(".")) {
            if (validIPV4Address(queryIP)) {
                return "IPv4";
            } else {
                return "Neither";
            }
        }
        return "Neither";
    }

    public static void main(String[] args) {
        Solution468 solution = new Solution468();
        System.out.println(solution.validIPAddress("172.16.254.1"));
        System.out.println(solution.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334"));
        System.out.println(solution.validIPAddress("256.256.256.256"));
        System.out.println(solution.validIPAddress("2001:0db8:85a3:0:0:8A2E:0370:7334:"));
        System.out.println(solution.validIPAddress("12..33.4"));
        System.out.println(solution.validIPAddress("1.1.1.1111111111111111111111111111111111111111111111111111"));
        System.out.println(solution.validIPAddress("20EE:FGb8:85a3:0:0:8A2E:0370:7334"));
    }
}
